package com.sm.service;

import com.sm.bo.VipActivitiesOrderBo;
import com.sm.bo.VipOrderBO;
import com.sm.dao.UserActivitiesMapper;
import com.sm.dao.VipActivitiesOrderMapper;
import com.sm.model.Order;
import com.sm.model.UserActivities;
import com.sm.model.UserVipOrder;
import com.sm.model.VipActivitiesOrder;
import com.sm.util.CommonUtil;
import com.sm.util.UUIDUtils;
import com.sm.wx.vo.AjaxResult;
import com.sm.wx.vo.WxOrderVO;
import com.sm.wx.wxpay.WxPay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class VipActivitiesOrderService {
    @Autowired
    private WxPay wxPay;
    @Autowired
    private VipActivitiesOrderMapper vipActivitiesOrderMapper;
    @Autowired
    private UserActivitiesMapper userActivitiesMapper;

    /**
     * 创建VIP订单
     *
     * @param vipOrder
     */

    @Transactional
    public AjaxResult createVipOrder(HttpServletRequest request, VipActivitiesOrderBo vipOrder) {

        //支付类型 1支付宝2微信
        Integer payType = vipOrder.getPayType();
        //创建订单
        VipActivitiesOrder activitiesOrder = new VipActivitiesOrder();
        activitiesOrder.setUserId(vipOrder.getUserId());
        activitiesOrder.setPrice(vipOrder.getPrice());
        activitiesOrder.setCreateTime(new Date());
        activitiesOrder.setPayTime(new Date());
        activitiesOrder.setIsPay(0);
        activitiesOrder.setUserActivitiesId(vipOrder.getUserActivitiesId());
        //该完整订单的订单号:
        String orderNum = "act" + CommonUtil.getOutTradeNo();
        activitiesOrder.setOrderNum(orderNum);//订单编号
        activitiesOrder.setPayType(vipOrder.getPayType());
        activitiesOrder.setActivitiesId(vipOrder.getActivitiesId());
        vipActivitiesOrderMapper.insert(activitiesOrder);
        if (payType == 2) {
            WxOrderVO wxOrderVO = new WxOrderVO("参会券订单", "1", activitiesOrder.getOrderNum(), activitiesOrder.getPrice().multiply(new BigDecimal("100")).toString(), "activitiesNotify.do");
            //微信支付
            return wxPay.appPay(request, wxOrderVO);
        } else {
            return new AjaxResult().addError("支付失败");
        }
    }

    /**
     * 获取订单是否支付完成
     * false代表未完成需要重新回调 true代表完成不需要回调
     *
     * @param orderNum
     * @return
     */
    public boolean getIsFinish(String orderNum) {
        VipActivitiesOrder activities = vipActivitiesOrderMapper.selectByOrderNum(orderNum);
        if (activities.getIsPay() == 0) {
            return false;
        }
        return true;
    }

    /**
     * 参会券回调订单状态
     *
     * @param orderNum
     */
    @Transactional
    public void notifyActivitiesOrder(String orderNum) {
        VipActivitiesOrder order = vipActivitiesOrderMapper.selectByOrderNum(orderNum);

        if (order == null) return;
        //添加参会券
        UserActivities userActivities = new UserActivities();
        //创建参会码
        String code = "a" + CommonUtil.getOutTradeNo();
        userActivities.setActivitiesCode(code);
        userActivities.setPayTime(new Date());
        userActivities.setActivitiesId(order.getActivitiesId());
        userActivities.setUserId(order.getUserId());
       // userActivitiesMapper.insert(userActivities);
        userActivitiesMapper.insertSelective(userActivities);
         order.setUserActivitiesId(userActivities.getUserActivitiesId());
        order.setPayTime(new Date());
        order.setIsPay(1);
        vipActivitiesOrderMapper.updateByPrimaryKeySelective(order);
    }
}
